package client.gui;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.table.DefaultTableModel;

import client.ClientController;
import dto.SongInfoDTO;

public class SearchSong extends JFrame {
	private JTextField user;
	private JPasswordField password;
	private JTextField searchArtist;
	private JButton findArtist;
	private JButton playSong;
	private JTable tableSongs;
	private DefaultTableModel defaultTable;
	private ClientController clientController;
	private String id = "";
	private String title = "";
	private String artist = "";
	private String sUser = "";
	private String sPass = "";
	
	
	public SearchSong (ClientController clientController, String user, String pass){
		sUser = user;
		sPass = pass;
		init ();
		this.clientController = clientController;
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setSize(800, 600);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
	}
	private void init (){
		user = new JTextField(20);
		user.setText(sUser);
		password = new JPasswordField(20);
		password.setText(sPass);
		searchArtist = new JTextField(20);
		findArtist = new JButton("Find Artist");
		findArtist.setDefaultCapable(true);
		playSong = new JButton("Play");
		tableSongs = new JTable();
		tableSongs.setAutoCreateRowSorter(true);
		tableSongs.setSize(50, 50);
		defaultTable = new DefaultTableModel(new String[] {"id","title","artist"},0);
		
		this.add(putPanel());
	
		findArtist.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				List<SongInfoDTO> list = new ArrayList<SongInfoDTO>();
				try {
					String author = searchArtist.getText();
					list = clientController.searchArtist(author);
					loadData(list);
					if (list.isEmpty()) {
						JOptionPane.showMessageDialog(rootPane, "There are no songs with the current filter");
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				
			}
		});
		playSong.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				int selectedRow = tableSongs.getSelectedRow();
				 id = (String) tableSongs.getModel().getValueAt(selectedRow, 0);
				 title = (String) tableSongs.getModel().getValueAt(selectedRow, 1);
				 artist = (String) tableSongs.getModel().getValueAt(selectedRow, 2);
				try {
					SwingUtilities.invokeLater(new Runnable() {
						@Override
						public void run() {
							try {
								new PlaySong(clientController, user.getText(), getPasswordString(), new SongInfoDTO(Integer.parseInt(id), title, artist));
							} catch (NumberFormatException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							} catch (RemoteException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
							
						}
					});
					
					
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				dispose();
				
			}
		});
		
		
	}
	private JPanel putPanel (){
		JPanel panel = new JPanel();
		panel.setLayout(null);
		JPanel panelUser = new JPanel(new FlowLayout(FlowLayout.LEFT));
		panelUser.setBounds(0, 0, 300, 90);
		panelUser.setBorder(BorderFactory.createTitledBorder("Users"));
		panelUser.add(user);
		panel.add(panelUser);
		
		JPanel panelPassword = new JPanel(new FlowLayout(FlowLayout.LEFT));
		panelPassword.setBounds(0, 95, 300, 90);
		panelPassword.setBorder(BorderFactory.createTitledBorder("Password"));
		panelPassword.add(password);
		panel.add(panelPassword);	
		
		JPanel panelSearch = new JPanel(new FlowLayout(FlowLayout.LEFT));
		panelSearch.setBounds(400, 95, 390, 90);
		panelSearch.setBorder(BorderFactory.createTitledBorder("Search Artist"));
		panelSearch.add(searchArtist);
		panelSearch.add(findArtist);
		panel.add(panelSearch);	
		
	//	JPanel tablePanel = new JPanel();
		tableSongs.setModel(defaultTable);
		JScrollPane sp = new JScrollPane(tableSongs);
//		tablePanel.add(sp);
		sp.setBounds(45, 200, 700, 300);
		panel.add(sp);
		
		JPanel button = new JPanel();
		button.setBounds(380, 520, 75, 50);
	
		button.add(playSong);
		
		panel.add(button);
		return panel;
	}
	
	private void loadData(List<SongInfoDTO> list) {
		while (defaultTable.getRowCount() > 0) //limpiar tabla
			defaultTable.removeRow(0);
		for (SongInfoDTO s : list){
			defaultTable.addRow(new String[] {Integer.toString(s.getId()),s.getTitle(),s.getArtist()});
		}
	}
	
	private String getPasswordString (){
		String r = "";
		for (char a : password.getPassword()){
			r += a;
		}
		return r;
	}
	
	public static void main (String [] args){

		try {
			for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
				if ("Nimbus".equals(info.getName())) {
					UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
		} catch (Exception e) {
		}
		SearchSong s = new SearchSong(null,"","");
	}
}
